/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.store.client.components;

import com.elf.gwt.common.client.model.ProductModel;
import com.elf.gwt.store.client.StoreComponentController;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineLabel;


/**
 * Tell someone you know about a product.  Only displayed when a
 * product is being displayed.
 */
public class TellAFriendComponent extends StoreSimpleComponent
{
	protected Grid subGrid;
	protected Image image;
	protected InlineLabel label = null;
	
	/**
	 * Constructor
	 */
	public TellAFriendComponent(StoreComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix, null, false);
		subGrid = null;
	}

	public void build()
	{
		if (subGrid == null)
		{
			subGrid = new Grid(2, 1);
			subGrid.addStyleName(getHandle() + "SubGrid");
			subGrid.getCellFormatter().addStyleName(1, 0, getHandle() + "Text");
			subGrid.getCellFormatter().addStyleName(1, 0, "ComponentLink");
			subGrid.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
			subGrid.getCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_CENTER);
			HorizontalPanel hpanel = new HorizontalPanel();
			image = new Image();
			image.addStyleName("ComponentLink");
			image.addClickHandler(clickHandler);
			image.setTitle(storeController.getStoreConstants().TellAFriendComponentTitle());
			label = new InlineLabel();
			label.addClickHandler(clickHandler);

			hpanel.add(image);
			subGrid.setWidget(0, 0, hpanel);
			subGrid.setWidget(1, 0, label);

			panel.setWidget(1, 0, subGrid);
			image.setUrl(getImageDirectory() + "button_tell_a_friend.gif");
			label.setText(storeController.getStoreConstants().TellAFriendComponentText());
		}
		onBuilt();
	}

	protected ClickHandler clickHandler = new ClickHandler() {
		public void onClick(ClickEvent event) {
			ProductModel productModel = StoreComponentController.productComponent.getProductModel();
			onSelection("TellAFriendEntryComponent--" + productModel.getId() + "--" + productModel.getCategoryId()
					+ "--" + StoreComponentController.manufacturersComponent.getManufacturerModel().getId());
		}
	};
}
